package ModelLayer;
import java.util.ArrayList;


/**
 * Write a description of class AddressContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AddressContainer
{
    private ArrayList<Person> persons;
    private static AddressContainer instance;
    
    private AddressContainer()
    {
        persons = new ArrayList<Person>();
    }
    
    public static AddressContainer getInstance()
    {  
        if (instance == null)
        {
            instance = new AddressContainer();
        }
        
        return instance;
    }
    
    public int listSize()
    {
        int size = persons.size();
        return size;
    }
        
    public void listAllPersons()
    {
        int index = 1;
        
        if (persons.size() < 1)
        {
            System.out.println("There is no persons in the address book...");
        }
        else
        {
            for(Person element : persons)
            {
                System.out.print(index+ ". ");
                element.printInformation();
                index++;
            }
        }
    }
    
    public void addPerson(String name, String lastName, String phone, String address, String zip, String city)
    {
        Person p = new Person(name, lastName, phone, address, zip, city);
        persons.add(p);
    }
    

    public Person findPerson(String phone)
    {
        int index = 0;
        int theIndex = 0;
        boolean found = false;
        Person foundPerson = null;
        if(persons.size() > 0)
        {
            while (index < persons.size() && !found)
            {
                if (persons.get(index).getPhone().equals(phone))
                {
                    theIndex = index;
                    found = true;
                }
        
                else
                {
                    index++;
                }
            }
            foundPerson = persons.get(theIndex);
        }
       
       return foundPerson;
    }
    
    public void updatePersonName(String phone, String name, String newName)
    {
        int index = 0;
        boolean found = false;
        Person foundPerson;
        
        while (index < persons.size() && !found)
        {
            if (persons.get(index).getName().equals(name) && persons.get(index).getPhone().equals(phone))
            {
                persons.get(index).setName(name);
                found = true;
            }
        
            else
            {
                index++;
            }
        }
    }
    
    public void updatePersonLastName(String phone, String lastName, String newLastName)
    {
        int index = 0;
        boolean found = false;
        Person foundPerson;
        
        while (index < persons.size() && !found)
        {
            if (persons.get(index).getLastName().equals(lastName) && persons.get(index).getPhone().equals(phone))
            {
                persons.get(index).setLastName(lastName);
                found = true;
            }
        
            else
            {
                index++;
            }
        }
    }
    
    public void updatePersonPhone(String phone, String newPhone)
    {
        int index = 0;
        boolean found = false;
        Person foundPerson;
        
        while (index < persons.size() && !found)
        {
            if (persons.get(index).getPhone().equals(phone))
            {
                persons.get(index).setPhone(phone);
                found = true;
            }
        
            else
            {
                index++;
            }
        }
    }
    
    public void updatePersonAddress(String phone, String address, String newAddress)
    {
        int index = 0;
        boolean found = false;
        Person foundPerson;
        
        while (index < persons.size() && !found)
        {
            if (persons.get(index).getAddress().equals(address) && persons.get(index).getPhone().equals(phone))
            {
                persons.get(index).setAddress(address);
                found = true;
            }
        
            else
            {
                index++;
            }
        }
    }
    
    public void updatePersonZip(String phone, String zip, String newZip)
    {
        int index = 0;
        boolean found = false;
        Person foundPerson;
        
        while (index < persons.size() && !found)
        {
            if (persons.get(index).getZip().equals(zip) && persons.get(index).getPhone().equals(phone))
            {
                persons.get(index).setZip(zip);
                found = true;
            }
        
            else
            {
                index++;
            }
        }
    }
    
    public void updatePersonCity(String phone, String city, String newCity)
    {
        int index = 0;
        boolean found = false;
        Person foundPerson;
        
        while (index < persons.size() && !found)
        {
            if (persons.get(index).getCity().equals(city) && persons.get(index).getPhone().equals(phone))
            {
                persons.get(index).setCity(city);
                found = true;
            }
        
            else
            {
                index++;
            }
        }
    }
    
    public void deletePersonByPhone(String phone)
    {
        int index = 0;
        boolean found = false;
        Person foundPerson;
        
        while (index < persons.size() && !found)
        {
            if (persons.get(index).getPhone().equals(phone))
            {
                persons.remove(index);
                found = true;
            }
        
            else
            {
                index++;
            }
        }
    }
}






















